package com.gcb.controller.businesses;

import com.gcb.common.annotation.Log;
import com.gcb.common.core.domain.AjaxResult;
import com.gcb.common.core.page.TableDataInfo;
import com.gcb.common.enums.BusinessType;
import com.gcb.common.utils.poi.ExcelUtil;
import com.gcb.controller.BaseController;
import com.gcb.domain.dto.BusinessesQueryDTO;
import com.gcb.domain.dto.BusinessesSaveDTO;
import com.gcb.domain.vo.BusinessesVO;
import com.gcb.service.IBusinessesService;
import com.gcb.framework.utils.SecurityUtils;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.util.List;

/**
 * 商户信息表
 *
 * @author linying
 * @since 2024-03-11
 */
@RestController
@RequestMapping(value = "/businesses")
public class BusinessesController extends BaseController {

    @Resource
    private IBusinessesService businessesService;

    /**
     * 新增
     *
     * @author linying
     * @date 2024/03/11
     **/
    @PreAuthorize("@ss.hasPermi('businesses:businesses:add')")
    @PostMapping("/add")
    public AjaxResult add(@Valid @RequestBody BusinessesSaveDTO dto) {
        Long userId = SecurityUtils.getUserId();
        businessesService.add(dto, userId);
        return success();
    }

    /**
     * 刪除
     *
     * @author linying
     * @date 2024/03/11
     **/
    @PreAuthorize("@ss.hasPermi('businesses:businesses:delete')")
    @GetMapping("/delete")
    public AjaxResult delete(Long id) {
        businessesService.delete(id);
        return success();
    }

    /**
     * 更新
     *
     * @author linying
     * @date 2024/03/11
     **/
    @PreAuthorize("@ss.hasPermi('businesses:businesses:update')")
    @PostMapping("/update")
    public AjaxResult update(@Valid @RequestBody BusinessesSaveDTO dto) {
        Long userId = SecurityUtils.getUserId();
        businessesService.update(dto, userId);
        return success();
    }

    /**
     * 查询 分页查询
     *
     * @author linying
     * @date 2024/03/11
     **/
    @PreAuthorize("@ss.hasPermi('businesses:businesses:pageList')")
    @GetMapping("/pageList")
    public TableDataInfo pageList(BusinessesQueryDTO dto) {
        startPage();
        List<BusinessesVO> list = businessesService.list(dto);
        return getDataTable(list);
    }

    /**
     * 获取商户列表
     *
     * @return
     */
    @PreAuthorize("@ss.hasPermi('businesses:businesses:list')")
    @GetMapping("/list")
    public AjaxResult list() {
        List<BusinessesVO> list = businessesService.list(new BusinessesQueryDTO());
        return success(list);
    }

    /**
     * 项目订单管理导出
     */
    @PreAuthorize("@ss.hasPermi('businesses:businesses:export')")
    @Log(title = "商户导出", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, BusinessesQueryDTO dto) {
        List<BusinessesVO> list = businessesService.list(dto);
        ExcelUtil<BusinessesVO> util = new ExcelUtil<>(BusinessesVO.class);
        util.exportExcel(response, list, "商户导出");
    }
}